/* 头部横向分类的数据共享 */
import { defineStore } from 'pinia'
import { getCategoryApi } from '@/api/category'
import { CatgoryItem, IApiRes } from '@/types/data'
import { topCategory } from '../constants'
const defaultCateItem = topCategory.map(item => { return { name: item } })
// console.log(defaultCateItem);

export default defineStore('category', {
  state: () => {
    return {
      list: defaultCateItem as CatgoryItem  // 分类导航的数据
    }
  },
  actions: {
    async getAllCategory(): Promise<void> {
      const res = await getCategoryApi()
      console.log(res);

      res.data.result.forEach((item) => {
        item.open = false
      })
      // console.log(res.data.result);
      this.list = res.data.result
    },
    show(id: string) {
      const listItem = this.list.filter(item => item.id == id)
      // console.log(listItem);
      listItem[0].open = true
    },
    hide(id: string) {
      const listItem = this.list.filter(item => item.id == id)
      console.log(listItem);
      listItem[0].open = false
    }
  }
})